import pytest
from sqlalchemy.orm import sessionmaker, scoped_session
from app.http.app import app as _app
from internal.extension.extension_db import db as _db


@pytest.fixture
def app():
    _app.config['TESTING'] = True
    return _app


@pytest.fixture
def client(app):
    with app.test_client() as client:
        yield client


@pytest.fixture
def db(app):
    with app.app_context():
        connect = _db.engine.connect()
        tr = connect.begin()
        session_factory = sessionmaker(bind=connect)
        session = scoped_session(session_factory)
        _db.session = session
        yield _db
        tr.rollback()
        tr.close()
        session.remove()
